<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\Request;
use app\api\services\admin\setting\RoleServices;
use think\App;

class RoleController extends BaseApi
{
    /**
     * @var \app\api\services\admin\setting\RoleServices
     */
    private RoleServices $services;

    public function __construct(Request $request, App $app, RoleServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * index  显示资源列表
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function select(Request $request): \think\Response
    {
        $where = $request->getParams([
            ['role_name', '', '', 'role_name_like'],
            ['delete_time', null],
            ['status'],
        ]);
        $this->validate($where, [
            'role_name_like' => 'max:32',
        ], [], true, true);

        $ret = $this->services->select($where);

        return ret_value(0, "", $ret['list'], 200, [], ["count" => $ret['count']]
        );
    }

    /**
     * 保存新建的资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function insert(Request $request): \think\Response
    {
        $data = $request->getParams([
            ['role_name', ''],
            ['description', ''],
            ['rules', ''],
        ]);

        $this->validate($data, [
            'role_name'   => 'require|chsDash|max:32',
            'description' => 'max:255',
        ]);

        $this->services->save($data);
        return ret_value(0, "Success");
    }

    /**
     * 显示指定的资源
     *
     * @param string|int $id
     *
     * @return \think\Response
     */
    public function get(string|int $id): \think\Response
    {
        $where = ['id' => $id];
        $ret   = $this->services->selectList($where, null, null, "id, role_name, rules, description, level, status")->toArray();
        return ret_value(0, "", $ret);
    }

    /**
     * 保存更新的资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function update(Request $request): \think\Response
    {
        $where = $request->getParams([['id']]);
        $data  = $request->getParams([
            ['id'],
            //['account'],
            ['role_name'],
            ['rules'],
            ['description'],
            ['status'],
        ]);
        $this->validate($where, [
            'id' => 'require|integer',
        ]);

        $this->validate($data, [
            'id'          => 'require|integer',
            'role_name'   => 'chsDash|max:32',
            'description' => 'max:255',
            //'rules' => '',
            'status'      => 'in:0,1',
        ]);

        $ret = $this->services->update($where, $data);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");

    }

    /**
     * 删除指定资源
     *
     * @param \app\Request $request
     *
     * @return \think\Response
     */
    public function delete(Request $request): \think\Response
    {
        $data = $request->getParams([
            ['data'],
        ]);

        $this->validate($data, [
            'data' => 'require',
        ]);

        $ids = explode(',', $data['data']);
        $ret = $this->services->destroy($ids);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

}
